﻿/*
============================================================================
文件名    ： User.cs
完全限定名： M_NET.Model.User
作者      ： 陈存腾
创建时间  ： 2012-03-16
修改时间  ： 
版本      ：
说明      ： 用于账户信息逻辑处理
============================================================================
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using M_NET.Common;
using System.Data;

namespace M_NET.Model
{
    /// <summary>
    /// 用户信息模型
    /// </summary>
    public class User
    {
        #region 私有字段

        /// <summary>
        /// 用户编号
        /// </summary>
        protected int user_no;

        /// <summary>
        /// 用户ID
        /// </summary>
        protected string user_id;

        /// <summary>
        /// 注册时间
        /// </summary>
        protected DateTime register_time;

        /// <summary>
        /// 上次登录时间
        /// </summary>
        protected DateTime last_login_time;

        /// <summary>
        /// 上次登录IP地址
        /// </summary>
        protected string last_login_ip;

        /// <summary>
        /// 用户姓名
        /// </summary>
        protected string user_name;

        /// <summary>
        /// 性别
        /// </summary>
        protected string user_gender;

        /// <summary>
        /// 出生日期
        /// </summary>
        protected DateTime user_birthday;

        /// <summary>
        /// 用户QQ
        /// </summary>
        protected string user_qq;

        /// <summary>
        /// 用户电子邮箱地址
        /// </summary>
        protected string user_email;

        /// <summary>
        /// 用户照片
        /// </summary>
        protected string photo_url;

        /// <summary>
        /// 籍贯（省、直辖市）
        /// </summary>
        protected string user_province;

        /// <summary>
        /// 市（县、区）
        /// </summary>
        protected string user_city;

        /// <summary>
        /// 镇（乡、街办）
        /// </summary>
        protected string user_town;

        #endregion

        #region 公有属性

        /// <summary>
        /// 用户编号
        /// </summary>
        public int No
        {
            get { return user_no; }
        }

        /// <summary>
        /// 用户ID
        /// </summary>
        public string ID
        {
            get { return user_id; }
        }

        /// <summary>
        /// 注册时间
        /// </summary>
        public DateTime RegisterTime
        {
            get { return register_time; }
        }

        /// <summary>
        /// 最后一次登录时间
        /// </summary>
        public DateTime LastLoginTime
        {
            get { return last_login_time; }
        }

        /// <summary>
        /// 最后一次登录IP
        /// </summary>
        public string LastLoginIP
        {
            get { return last_login_ip; }
        }

        /// <summary>
        /// 用户姓名
        /// </summary>
        public string Name
        {
            get { return user_name; }
            set { user_name = value; }
        }

        /// <summary>
        /// 性别
        /// </summary>
        public string Gender
        {
            get { return user_gender; }
            set { user_gender = value; }
        }

        /// <summary>
        /// 出生日期
        /// </summary>
        public DateTime Birthday
        {
            get { return user_birthday; }
            set { user_birthday = value; }
        }

        /// <summary>
        /// 用户QQ
        /// </summary>
        public string QQ
        {
            get { return user_qq; }
            set { user_qq = value; }
        }

        /// <summary>
        /// 用户电子邮箱地址
        /// </summary>
        public string Email
        {
            get { return user_email; }
            set { user_email = value; }
        }

        /// <summary>
        /// 用户照片
        /// </summary>
        public string PhotoUrl
        {
            get { return photo_url; }
            set { photo_url = value; }
        }

        /// <summary>
        /// 籍贯（省、直辖市）
        /// </summary>
        public string Province
        {
            get { return user_province; }
            set { user_province = value; }
        }

        /// <summary>
        /// 市（县、区）
        /// </summary>
        public string City
        {
            get { return user_city; }
            set { user_city = value; }
        }

        /// <summary>
        /// 镇（乡、街办）
        /// </summary>
        public string Town
        {
            get { return user_town; }
            set { user_town = value; }
        }

        #endregion

        #region 静态成员

        /// <summary>
        /// 当前系统登录用户
        /// </summary>
        public static User Current = null;

        #endregion

        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public User()
        {
            register_time = DateTime.MinValue;
            user_birthday = DateTime.MinValue;
            last_login_time = DateTime.MinValue;
        }

        /// <summary>
        /// 根据info字符串构造
        /// </summary>
        public User(string info)
        {
            this.register_time = DateTime.MinValue;
            this.last_login_time = DateTime.MinValue;
            this.user_birthday = DateTime.MinValue;
            this.parse(info);
        }

        /// <summary>
        /// 根据DataTable对象构造
        /// </summary>
        public User(DataTable dataTable)
        {
            this.register_time = DateTime.MinValue;
            this.last_login_time = DateTime.MinValue;
            this.user_birthday = DateTime.MinValue;
            this.parse(dataTable);
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 从 info 字符串中解析 User 对象
        /// </summary>
        /// <param name="info">info字符串</param>
        private void parse(string info)
        {
            try
            {
                info = info.Trim();
                if (info[info.Length - 1] != ',')
                {
                    info = String.Concat(info, ',');
                }

                this.user_no = StringHelper.GetIntByKey(info, "user_no");
                this.user_id = StringHelper.GetValueByKey(info, "user_id", true);
                this.register_time = StringHelper.GetDateTimeByKey(info, "register_time");
                this.last_login_time = StringHelper.GetDateTimeByKey(info, "last_login_time");
                this.last_login_ip = StringHelper.GetValueByKey(info, "last_login_ip", true);
                this.user_name = StringHelper.GetValueByKey(info, "user_name", true);
                this.user_gender = StringHelper.GetValueByKey(info, "user_gender", true);
                this.user_birthday = StringHelper.GetDateTimeByKey(info, "user_birthday");
                this.user_qq = StringHelper.GetValueByKey(info, "user_qq", true);
                this.user_email = StringHelper.GetValueByKey(info, "user_email", true);
                this.user_province = StringHelper.GetValueByKey(info, "user_province", true);
                this.user_city = StringHelper.GetValueByKey(info, "user_city", true);
                this.user_town = StringHelper.GetValueByKey(info, "user_town", true);
                this.photo_url = StringHelper.GetValueByKey(info, "photo_url", true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 从 dataTable 中解析 User 对象
        /// </summary>
        /// <param name="dataTable">包含用户信息的dataTable</param>
        private void parse(DataTable dataTable)
        {
            if (dataTable.Rows.Count <= 0)
            {
                return;
            }
            try
            {
                if (dataTable.Columns.Contains("user_no") && !(dataTable.Rows[0]["user_no"] is System.DBNull))
                {
                    this.user_no = (int)dataTable.Rows[0]["user_no"];
                }
                if (dataTable.Columns.Contains("user_id"))
                {
                    this.user_id = dataTable.Rows[0]["user_id"] as string;
                }
                if (dataTable.Columns.Contains("register_time") && !(dataTable.Rows[0]["register_time"] is System.DBNull))
                {
                    this.register_time = (DateTime)dataTable.Rows[0]["register_time"];
                }
                if (dataTable.Columns.Contains("last_login_time") && !(dataTable.Rows[0]["last_login_time"] is System.DBNull))
                {
                    this.last_login_time = (DateTime)dataTable.Rows[0]["last_login_time"];
                }
                if (dataTable.Columns.Contains("last_login_ip"))
                {
                    this.last_login_ip = dataTable.Rows[0]["last_login_ip"] as string;
                }
                if (dataTable.Columns.Contains("user_name"))
                {
                    this.user_name = dataTable.Rows[0]["user_name"] as string;
                }
                if (dataTable.Columns.Contains("user_gender"))
                {
                    this.user_gender = dataTable.Rows[0]["user_gender"] as string;
                }
                if (dataTable.Columns.Contains("user_birthday") && !(dataTable.Rows[0]["user_birthday"] is System.DBNull))
                {
                    this.user_birthday = (DateTime)dataTable.Rows[0]["user_birthday"];
                }
                if (dataTable.Columns.Contains("user_qq"))
                {
                    this.user_qq = dataTable.Rows[0]["user_qq"] as string;
                }
                if (dataTable.Columns.Contains("user_email"))
                {
                    this.user_email = dataTable.Rows[0]["user_email"] as string;
                }
                if (dataTable.Columns.Contains("user_province"))
                {
                    this.user_province = dataTable.Rows[0]["user_province"] as string;
                }
                if (dataTable.Columns.Contains("user_city"))
                {
                    this.user_city = dataTable.Rows[0]["user_city"] as string;
                }
                if (dataTable.Columns.Contains("user_town"))
                {
                    this.user_town = dataTable.Rows[0]["user_town"] as string;
                }
                if (dataTable.Columns.Contains("photo_url"))
                {
                    this.photo_url = dataTable.Rows[0]["photo_url"] as string;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 从 xml 字符串中解析 User 对象
        /// </summary>
        /// <param name="xml">包含用户信息的 xml 字符串</param>
        private void parseByXML(string xml)
        {
            try
            {
                System.Xml.XmlDocument xmlInfo = new System.Xml.XmlDocument();
                xmlInfo.LoadXml(xml);
                foreach (System.Xml.XmlNode item in xmlInfo.DocumentElement.ChildNodes)
                {
                    switch (item.Name)
                    {
                        case "user_no":
                            this.user_no = int.Parse(item.InnerText);
                            break;
                        case "user_id":
                            this.user_id = item.InnerText;
                            break;
                        case "register_time":
                            this.register_time = DateTime.Parse(item.InnerText);
                            break;
                        case "last_login_time":
                            this.last_login_time = DateTime.Parse(item.InnerText);
                            break;
                        case "last_login_ip":
                            this.last_login_ip = item.InnerText;
                            break;
                        case "user_name":
                            this.user_name = item.InnerText;
                            break;
                        case "user_gender":
                            this.user_gender = item.InnerText;
                            break;
                        case "user_birthday":
                            this.user_birthday = DateTime.Parse(item.InnerText);
                            break;
                        case "user_qq":
                            this.user_qq = item.InnerText;
                            break;
                        case "user_email":
                            this.user_email = item.InnerText;
                            break;
                        case "photo_url":
                            this.photo_url = item.InnerText;
                            break;
                        case "user_province":
                            this.user_province = item.InnerText;
                            break;
                        case "user_city":
                            this.user_city = item.InnerText;
                            break;
                        case "user_town":
                            this.user_town = item.InnerText;
                            break;
                    }

                }
            }
            catch
            {
            }
        }

        #endregion

        #region 公共方法

        /// <summary>
        /// 为 User 对象添加信息值
        /// </summary>
        /// <returns>操作是否成功</returns>
        public bool AddInfo(string info)
        {
            try
            {
                this.parse(info);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 为 User 对象添加信息值
        /// </summary>
        /// <returns>操作是否成功</returns>
        public bool AddInfo(DataTable dataTable)
        {
            try
            {
                this.parse(dataTable);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 把 User 对象格式化成 info 字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return String.Concat(
                StringHelper.SetValueByKey("user_no", user_no.ToString()),
                StringHelper.SetValueByKey("user_id", user_id, true),
                register_time == DateTime.MinValue ? null : StringHelper.SetValueByKey("register_time", register_time.ToString()),
                last_login_time == DateTime.MinValue ? null : StringHelper.SetValueByKey("last_login_time", last_login_time.ToString()),
                StringHelper.SetValueByKey("last_login_ip", last_login_ip, true),
                StringHelper.SetValueByKey("user_name", user_name, true),
                StringHelper.SetValueByKey("user_gender", user_gender, true),
                user_birthday == DateTime.MinValue ? null : StringHelper.SetValueByKey("user_birthday", user_birthday.ToString()),
                StringHelper.SetValueByKey("user_qq", user_qq, true),
                StringHelper.SetValueByKey("user_email", user_email, true),
                StringHelper.SetValueByKey("photo_url", photo_url, true),
                StringHelper.SetValueByKey("user_province", user_province, true),
                StringHelper.SetValueByKey("user_city", user_city, true),
                StringHelper.SetValueByKey("user_town", user_town, true)
            );
        }

        /// <summary>
        /// 把用户信息格式化成 xml 形式
        /// </summary>
        /// <returns></returns>
        public string ToXMLString()
        {
            return String.Concat(
                "<user>",
                StringHelper.SetXMLValueByKey("user_no", user_no.ToString(), false),
                StringHelper.SetXMLValueByKey("user_id", user_id, true),
                register_time == DateTime.MinValue ? null : StringHelper.SetXMLValueByKey("register_time", register_time.ToString(), false),
                last_login_time == DateTime.MinValue ? null : StringHelper.SetXMLValueByKey("last_login_time", last_login_time.ToString(), false),
                StringHelper.SetXMLValueByKey("last_login_ip", last_login_ip, true),
                StringHelper.SetXMLValueByKey("user_name", user_name, true),
                StringHelper.SetXMLValueByKey("user_gender", user_gender, true),
                user_birthday == DateTime.MinValue ? null : StringHelper.SetXMLValueByKey("user_birthday", user_birthday.ToString(), false),
                StringHelper.SetXMLValueByKey("user_qq", user_qq, true),
                StringHelper.SetXMLValueByKey("user_email", user_email, true),
                StringHelper.SetXMLValueByKey("photo_url", photo_url, true),
                StringHelper.SetXMLValueByKey("user_province", user_province, true),
                StringHelper.SetXMLValueByKey("user_city", user_city, true),
                StringHelper.SetXMLValueByKey("user_town", user_town, true),
                "</user>"
            );
        }

        #endregion
    }
}
